class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(object):
    def isValidSequence(self, root, arr):
        n = len(arr)

        def search(node, index):
            if not node:
                return False
            else:
                if node.val != arr[index]:
                    return False
                else:
                    if index == n - 1:
                        if not node.left and not node.right:
                            return True
                        else:
                            return False
                    else:
                        return search(node.left, index + 1) or search(node.right, index + 1)

        return search(root, 0)